from DrissionPage import ChromiumPage
from time import sleep
import re
import pandas as pd
city_list = [
    "广州市", "东莞市", "江门市", "珠海市", "中山市", "阳江市", "河源市", "潮州市", "汕头市", "深圳市",
    "湛江市", "佛山市", "肇庆市", "云浮市", "揭阳市", "汕尾市", "惠州市"
]
keyword = 'FSU离线告警'  # 要筛选的关键字
keyword_count = 0  # 用于统计关键字出现次数
column_titles = ['地市', 'FSU告警数量', '其他告警数量', '告警总数']
list = []
data_list = []
sis_list = []

page = ChromiumPage()
page.listen.start("navigation")
page.get('http://47.107.237.125:8012/#/login')
page.ele('@id=userName').input('zhoukejian')
page.ele('@id=password').input('123456\n')  #实时定位数据，不缓存
try:
    page.ele('@type=submit').click()
except:
    pass
sleep(2)
page.get('http://47.107.237.125:8012/#/alarm-management/alarm-list')
selected_button = page.eles('@class=ant-select-selection-selected-value')
for city in city_list:
    print(f"正在选择地市：{city}")
    selected_button[2].click().input(f'{city}\n').wait(2)
    button = page.eles('@type=button')
    button[0].click()
    sleep(2)
    while True:
        Tbody = page.eles('@class=ant-table-tbody')
        tr_list = Tbody[0].eles('t:tr')
        for i in tr_list:
            td_list = i.eles('t:td')
            if keyword in td_list[6].text:
                keyword_count += 1  # 统计关键字出现次数
                data_list.append([
                    td_list[1].text, td_list[3].text, td_list[4].text,
                    td_list[5].text, td_list[6].text, td_list[7].text
                ])
                list.append(data_list)
                data_list = []
        if keyword_count == 0:
            print('没有找到相关告警')
        else:
            for i in list:
                print(i)
        list.clear()
        next_page = page.ele('t:li@@title=下一页')
        if 'false' in next_page.attr('aria-disabled'):
            next_page.click()
            sleep(1)
        else:
            next_page = page.ele('t:li@@title=1')
            next_page.click()
            break
    page_total = page.ele('@class=ant-pagination-total-text')
    totals = re.findall(r'\d+', page_total.text)  #list
    other = int(totals[0]) - int(keyword_count)
    anylysis_list = [city, keyword_count, other, totals[0]]
    sis_list.append(anylysis_list)
    keyword_count = 0
    other = 0
    sleep(2)

df_dynamic = pd.DataFrame(sis_list, columns=column_titles)
df_dynamic.to_excel("旧辅助监控.xlsx", index=False)
sleep(1)
page.quit()
